package com.jp.algorithm;

import java.util.ArrayList;
import java.util.List;

public class BackTrace {

    public static void backTrace(int[] a,int start, List<Integer> tmp,int target,List<List<Integer>> results){

        if(target<0){
            return;
        }

        if(target==0){
            List<Integer> result = new ArrayList<>(tmp);
            results.add(result);
            return;
        }

        for(int i=start;i<a.length;i++){
            tmp.add(a[i]);
            backTrace(a,0,tmp,target-a[i],results);
            tmp.remove(tmp.size()-1);
        }
    }


    public static void test(){
        List<Integer> tmp= new ArrayList<>();
        int[] a = {1,2,3,4,5,6,7,8};
        int target = 5;
        List<List<Integer>> results =new ArrayList<>();
        backTrace(a,0,tmp,target,results);
        for (List<Integer> result:results){
            System.out.println(result);
        }
    }

    public static void main(String[] args) {
        test();
    }
}
